//entroll.js
//获取应用实例
const app = getApp()

Page({
  data: {
    pInfo: {}, 
    name: "",
    phone: "",
    msg: "",
  },

  onLoad:function(e){
    var that=this;


    this.setData({
      id: e.id
    })
    wx.request({
      url: 'https://soya.91soya.com/api/proinfo/' + e.id + "/" + app.globalData.openId,
      method: "get",
      success: function (res) {
        var resp = res.data.data;
        console.log(resp)
        that.setData({
          pInfo: {
            id: resp.pid,
            pImg: " https://soya.91soya.com/uploads/" + resp.p_img,
            pName: resp.pname,
            yPrice: resp.y_price,
            price: resp.price,
            tImg: " https://soya.91soya.com/uploads/" + resp.t_img,
            tName: resp.tname,
            address: "上海市静安寺区万航渡路83号金航大厦16楼",
            pNum:1
          },
        })
        wx.request({
          url: 'https://soya.91soya.com/api/wxpay/',
          method: "post",
          data: {
            name: that.data.pInfo.pName,
            pid: that.data.pInfo.id,
            openid: app.globalData.openId,
            price: that.data.pInfo.price,
          },
          success: function (res) {
            console.log(res.data)
            var data = res.data;
            that.setData({
              timeStamp: data.timeStamp.toString(),
              nonceStr: data.nonceStr,
              package: data.package,
              signType: data.signType,
              paySign: data.paySign,
              order_id: data.order_id
            })

          },
          fail: function () {
            console.log("预订发送请求失败！")
          }
        })
      },
      fail: function () {
        console.log("产品数据请求失败")
      }
    })
    

  },
  Name: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  Phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  Msg: function (e) {
    this.setData({
      msg: e.detail.value
    })
  },
  submit: function () {
    var that=this;
    var name = this.data.name;
    var phone = this.data.phone;
    var msg = this.data.msg;
    var phoneOk = /^[1][3,4,5,7,8][0-9]{9}$/.test(phone);
    var openId = app.globalData.openId;
    if (name != "" && phoneOk) {
      wx.request({
        url: "https://soya.91soya.com/api/link",
        method: "post",
        data: {
          "name": name,
          "phone": phone,
          "msg": msg,
          "pid": that.data.pInfo.id,
          "openId": openId
        },
        success: function () {
          console.log("请求成功")
          that.pay()
        },
        fail: function () {
          console.log("提交信息错误");
          
        }

      })
    } else {
      wx.showModal({
        title: '信息错误！',
        content: '请填写正确的姓名和手机号！',
        showCancel: false
      })
    }

  },
  pay:function(){
    console.log("pay 调用")
    var that=this;
    wx.requestPayment({
      timeStamp: that.data.timeStamp,
      nonceStr: that.data.nonceStr,
      package: that.data.package,
      signType: that.data.signType,
      paySign: that.data.paySign,
      success: function (res) {
        console.log('付款成功')
        wx.request({
          url: 'https://soya.91soya.com/api/wxpayupdate/',
          method: "post",
          data: {
            timeStamp: that.data.timeStamp.toString(),
            order_id: that.data.order_id
          },
          success: function () {
            console.log("传订单状态")
          }
        })
      },
      fail: function (res) {
        console.log('付款失败')
      }
    })
  }

})
